package cn.iocoder.an.module.mold.controller.admin.rechecktask2item;

import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.rechecktask2item.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.rechecktask2item.RecheckTask2itemDO;
import cn.iocoder.an.module.mold.service.rechecktask2item.RecheckTask2itemService;

@Tag(name = "管理后台 - 模具复检任务以及复检项目对照")
@RestController
@RequestMapping("/mold/recheck-task2item")
@Validated
public class RecheckTask2itemController {

    @Resource
    private RecheckTask2itemService recheckTask2itemService;

    @PostMapping("/create")
    @Operation(summary = "创建模具复检任务以及复检项目对照")
    @PreAuthorize("@ss.hasPermission('mold:recheck-task2item:create')")
    public CommonResult<Long> createRecheckTask2item(@Valid @RequestBody RecheckTask2itemSaveReqVO createReqVO) {
        return success(recheckTask2itemService.createRecheckTask2item(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具复检任务以及复检项目对照")
    @PreAuthorize("@ss.hasPermission('mold:recheck-task2item:update')")
    public CommonResult<Boolean> updateRecheckTask2item(@Valid @RequestBody RecheckTask2itemSaveReqVO updateReqVO) {
        recheckTask2itemService.updateRecheckTask2item(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具复检任务以及复检项目对照")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:recheck-task2item:delete')")
    public CommonResult<Boolean> deleteRecheckTask2item(@RequestParam("id") Long id) {
        recheckTask2itemService.deleteRecheckTask2item(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具复检任务以及复检项目对照")
                @PreAuthorize("@ss.hasPermission('mold:recheck-task2item:delete')")
    public CommonResult<Boolean> deleteRecheckTask2itemList(@RequestParam("ids") List<Long> ids) {
        recheckTask2itemService.deleteRecheckTask2itemListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具复检任务以及复检项目对照")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:recheck-task2item:query')")
    public CommonResult<RecheckTask2itemRespVO> getRecheckTask2item(@RequestParam("id") Long id) {
        RecheckTask2itemDO recheckTask2item = recheckTask2itemService.getRecheckTask2item(id);
        return success(BeanUtils.toBean(recheckTask2item, RecheckTask2itemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具复检任务以及复检项目对照分页")
    @PreAuthorize("@ss.hasPermission('mold:recheck-task2item:query')")
    public CommonResult<PageResult<RecheckTask2itemRespVO>> getRecheckTask2itemPage(@Valid RecheckTask2itemPageReqVO pageReqVO) {
        PageResult<RecheckTask2itemDO> pageResult = recheckTask2itemService.getRecheckTask2itemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecheckTask2itemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具复检任务以及复检项目对照 Excel")
    @PreAuthorize("@ss.hasPermission('mold:recheck-task2item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecheckTask2itemExcel(@Valid RecheckTask2itemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecheckTask2itemDO> list = recheckTask2itemService.getRecheckTask2itemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具复检任务以及复检项目对照.xls", "数据", RecheckTask2itemRespVO.class,
                        BeanUtils.toBean(list, RecheckTask2itemRespVO.class));
    }

    @GetMapping("/items-of-task")
    @Operation(summary = "获得模具保养任务以及保养项目对照")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TransMethodResult
    public CommonResult<List<RecheckTask2itemRespVO>> getRecheckTask2itemByTaskId(@RequestParam("id") Long taskId) {
        List<RecheckTask2itemDO> items = recheckTask2itemService.getRecheckTask2itemListByTaskId(taskId);
        return success(BeanUtils.toBean(items, RecheckTask2itemRespVO.class));
    }
}